package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.JobService;
import service.MovieService;
import util.RegexUtil;

import domain.ActorJob;
import domain.CrewJob;
import domain.Movie;
import ex.UnparsableToDoubleException;
import ex.UnparsableToIntegerException;


public class MovieProfile extends HttpServlet {
	MovieService movieService=new MovieService();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		
		try{
			String action=request.getParameter("which");
			if(action!=null){
				if(RegexUtil.isParsableToInt(action)){
					long mid=Long.parseLong(action);
					domain.Movie movie=movieService.getMovieByMid(mid);
					request.setAttribute("movie", movie);
				}
				else{
					response.sendRedirect("home");
				}			
			}
			else{
				response.sendRedirect("home");
			}
			
			if(request.getAttribute("movie")!=null)		{
				
				Movie movie=(Movie)request.getAttribute("movie");
				if(movie.isCompleted()){
					
					RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/user/movie-show.jsp");
					if (dispatcher != null) dispatcher.forward(request, response);
					
				}
				else{
					JobService jService=new JobService();
					List<ActorJob> actors= jService.getActorJobsByMovie(movie);
					List<CrewJob> crews= jService.getCrewJobsByMovie(movie);
					
					request.setAttribute("actorjobs", actors);
					request.setAttribute("crewjobs", crews);
					
					
					RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/user/show-project.jsp");
					if (dispatcher != null) dispatcher.forward(request, response);
				}
			}
			else{
				response.sendRedirect("home");
			}
		}
		//catch (DatabaseException e) {
		//	request.setAttribute("error", e.getMessage());
		//}
		catch (Exception e) {
			request.setAttribute("error", "A general error occured");
		}
		finally
		{
			response.sendRedirect("home");
		}
	}


	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name");
		String genre=request.getParameter("genre");
		String year=request.getParameter("year");
		String runTimeStr=request.getParameter("runTime");
		String summary=request.getParameter("summary");
		//String image=request.getParameter("image");TODO
		String poster=request.getParameter("poster");
		String linkToMovie=request.getParameter("link");
		String ratingStr=request.getParameter("rating");
		String popularityStr=request.getParameter("popularity");
		
/*		try {
			RegexUtil.isParsableToInt(runTimeStr);
		} catch (UnparsableToIntegerException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			RegexUtil.isParsableToInt(popularityStr);
		} catch (UnparsableToIntegerException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			RegexUtil.isParsableToDouble(ratingStr);
		} catch (UnparsableToDoubleException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*///TODO_BURAK uncomment
		
		int runTime = Integer.parseInt(runTimeStr);
		int popularity = Integer.parseInt(popularityStr);
		double rating = Double.parseDouble(ratingStr);
		
		/*Movie movie=new Movie((long)-1,name,genre,year,summary,runTime,poster,linkToMovie,rating,popularity,false,null,null);
		
		movieService.createMovie(movie);
		
        HttpSession session = request.getSession();
		session.setAttribute("movie", movie);*///TODO_BURAK uncomment
		
		response.sendRedirect("movie?who=dmyMovie");
		
//		RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/user/register.jsp");
//		 if (dispatcher != null) dispatcher.forward(request, response);
	}

}